/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facades;

import entities.User;
import entities.UserToken;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author damfercer
 */
@Stateless
public class UserTokenFacade extends AbstractFacade<UserToken> {
    @PersistenceContext(unitName = "ProyectoPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserTokenFacade() {
        super(UserToken.class);
    }
    
    public void removeAllUserToken(User u){
        Query q=em.createNamedQuery("UserToken.findByUser");
        q.setParameter("idUser", u.getIdUser());
        List<UserToken> userTokens=q.getResultList();
        for (UserToken userToken : userTokens) {
            this.remove(userToken);
        }
    }
    
    public User findByToken(String token){
        User user=null;
        Query q=em.createNamedQuery("UserToken.findByToken",UserToken.class);
        q.setParameter("token", token);
        List<UserToken> userTokens=q.getResultList();
        if(userTokens!=null && userTokens.size()>0){
            user=userTokens.get(0).getUser();
        }
        return user;
    }
    
}
